//
//  XJMyWalletTableHeaderView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/23.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJMyWalletTableHeaderView.h"
@interface XJMyWalletTableHeaderView()

@property(nonatomic, strong) UIImageView *bgImageView; // 注意这是一个iv 一定义要打开用户交互
@property(nonatomic, strong) UILabel *nameLabel; // 文字:当前余额
@property(nonatomic, strong) UILabel *balanceLabel; //余额label
@property(nonatomic, strong) UIButton *chargeButton; //充值按钮

@end

@implementation XJMyWalletTableHeaderView

#pragma mark - 一 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView{
    [self addSubview:self.bgImageView];
    
    [self.bgImageView addSubview:self.nameLabel];
    [self.bgImageView addSubview:self.balanceLabel];
    [self.bgImageView addSubview:self.chargeButton];
}

#pragma mark - 二 lazy
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.image = [UIImage imageNamed:@"bj_wallet"];
        _bgImageView.userInteractionEnabled = YES; //✅ 打开用户交互。
    }
    return _bgImageView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"当前余额(元)";
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.font = [UIFont systemFontOfSize:17.0];
    }
    return _nameLabel;
}

- (UILabel *)balanceLabel {
    if (!_balanceLabel) {
        _balanceLabel = [[UILabel alloc] init];
        _balanceLabel.font = XJFONTSET_M(50);
        _balanceLabel.text  = @"00.00";
        _balanceLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _balanceLabel;
}

- (UIButton *)chargeButton {
    if (!_chargeButton) {
        _chargeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_chargeButton setTitle:@"充值" forState:(UIControlStateNormal)];
        [_chargeButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        _chargeButton.layer.masksToBounds = YES;
        _chargeButton.layer.cornerRadius = 10;
        _chargeButton.layer.borderWidth = 1.0;
        _chargeButton.layer.borderColor = [UIColor blackColor].CGColor;
        
        [_chargeButton addTarget:self action:@selector(chargeButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _chargeButton;
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.mas_top).offset(20);
        make.right.equalTo(self.mas_right).offset(-15);
        make.bottom.equalTo(self.mas_bottom).offset(-30);
    }];
    
    // 垂直居中
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgImageView.mas_top).offset(20); //15-->20
        make.centerX.equalTo(self.bgImageView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(150, 20));
    }];
    
    [self.balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(20);
        make.centerX.equalTo(self.bgImageView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(280, 50));
    }];
    
    [self.chargeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.balanceLabel.mas_bottom).offset(20);
        make.centerX.equalTo(self.bgImageView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
}

#pragma mark - 四 回调事件
- (void)chargeButtonAction{
//    XJLog(@"充值");
    if (self.myWalletTableViewHeaderVeiwChargeBlock) {
        self.myWalletTableViewHeaderVeiwChargeBlock();
    }
}

- (void)congifureMyWalletHeaderViewWithBalance:(NSNumber *)balance {
    NSString *str = [NSString stringWithFormat:@"%.2f",[balance floatValue]];
    
    self.balanceLabel.text = str;
}




@end
